<?php defined('SYSPATH') OR die('No direct access allowed.');

class Register_Controller extends Master_Home_Controller 
{
	public function index() {		
		$view = new View('member/register');
		$this->set_title('ลงทะเบียน');
		$captcha = new Captcha;
		
		$form = array (
					'txtEmail'    => '',
					'txtUsername'   => '',
					'txtPassword' => '',
					'cboPrefix' => '',
					'txtFirstname' => '',
					'txtLastname' => '',
					'txtBirthdate' => '',
					'rdoSex' => '',
					'cboQuestion' => '',
					'txtAnswer' => '',
					'rdoMemberType' => '',
					'txtCaptcha' => '',
					'txtPicture' => '',
				);
		$view->form = $form;
		$view->status = '';
		$view->errors = array();
		$view->captcha = $captcha;
		$this->load_master($view);
		
		$this->template->content = $view;
		$this->template->render(TRUE);
	}
	
	public function load_master($view) {
		$master = new Masters_Model();
		
		// Get prefix master data
		$view->master_prefixs = $master->browse(2);
		// Get question master data
		$view->master_questions = $master->browse(3);
		// Get sex master data
		$view->master_sexs = $master->browse(4);
		// Get member type master data
		$view->master_member_types = $master->browse(5);
	}
	
	public function save() {
    	$view = new View('member/register');

		$this->set_title('ลงทะเบียน');

		if($_POST) {
    		$post = new Validation($_POST);
			$post->pre_filter('trim', TRUE);
            $post->add_rules('cboPrefix','required');
			$post->add_rules('txtFirstname','required', 'length[2,100]');
            $post->add_rules('txtLastname','required', 'length[2,150]');
            $post->add_rules('txtBirthdate','valid::date');
            $post->add_rules('rdoSex','required');
            $post->add_rules('txtUsername','required', 'length[3,32]');
            $post->add_rules('txtPassword','required', 'length[3,50]');
		    $post->add_rules('txtEmail', 'required', 'valid::email');
		    $post->add_rules('cboQuestion','required');
		    $post->add_rules('txtAnswer','required', 'length[2,100]');
		    $post->add_rules('rdoMemberType','required');
		    $post->add_rules('txtCaptcha','required', 'Captcha::valid');
        	$post->post_filter('ucfirst', 'name');
			$form = $post->as_array();
			
			if (!$post->validate())
	        {
	        	$errors = $post->errors('profile_error');
	        	$view->errors = $errors;
	        	
	        	$view->status = '';
	        }
	        else {
				$profiles = new Profiles_Model;
        		$profiles->file_validate('txtPicture');
        		$filename = $profiles->get_filename();
				
                $view->errors= array();

                if (!empty($filename)) {
		        	$form['txtPicture'] = str_replace(DOCROOT, '', $profiles->get_filename());
        		}
                
        		$txtPicture = '';
        		if (isset($form['txtPicture'])) {
        			$txtPicture = $form['txtPicture'];
        		}
        		
                $data = array (
					'email'    => $form['txtEmail'],
					'username'   => $form['txtUsername'],
					'password' => $form['txtPassword'],
					'prefix_id' => $form['cboPrefix'],
					'firstname' => $form['txtFirstname'],
					'lastname' => $form['txtLastname'],
					'birthdate' => $form['txtBirthdate'],
					'sex' => $form['rdoSex'],
					'question_id' => $form['cboQuestion'],
					'answer' => $form['txtAnswer'],
					'member_type' => $form['rdoMemberType'],
                	'picture' => $txtPicture,
				);
        		
	        	if ($profiles->register($data)) {
	        		$view->status = Kohana::lang('profile_error.save_result.save_completed');
	        	}
	        	else {
	        		$view->status = Kohana::lang('profile_error.save_result.save_failed');
	        	}
	        }
	        $view->form = $form;
	        $this->load_master($view);
	        $captcha = new Captcha;
	        $view->captcha = $captcha;
	       	$this->template->content = $view;
			$this->template->render(TRUE);
    	}
    }
    
}
